//
//  SearchProjectShaiXuan.m
//  HappyStore
//
//  Created by 李小辉 on 16/5/26.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "SearchPriceShaiXuan.h"

@interface SearchPriceShaiXuan ()<UIScrollViewDelegate>{
    UIButton *btnHY;
    UIButton *btnPrice;
    UIButton *btnAddress;
    
    NSString *strHY;
    NSString *strPrice;
    NSString *strAddress;
}
@property (weak, nonatomic) IBOutlet UIScrollView *scrollview;

// 数据源
@property (strong, nonatomic) NSArray *industryArr;// 行业
@property (strong, nonatomic) NSArray *priceArr; // 金额
@property (strong, nonatomic) NSArray *addressArr;// 地区

@end

@implementation SearchPriceShaiXuan
#pragma mark - 初始化
-(NSArray *)industryArr{
    if (!_industryArr) {
        
        _industryArr = @[@"个人资金",@"企业资金",@"小额贷款",@"天使投资",
                         @"VC投资",@"PE投资",@"基金公司",@"保险公司",
                         @"投资公司",@"担保公司",@"证券公司",@"信托公司",
                         @"资产管理",@"资产管理",@"商业银行"];
    }
    return _industryArr;
}

-(NSArray *)priceArr{
    if (!_priceArr) {
        
        _priceArr = @[@"不限",@"50万以下",@"50-100万",@"100-500万",@"500-1000万",@"1000-5000万",@"5000万－1亿",@"1-10亿",@"10亿以上"];
    }
    
    return _priceArr;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    titleLabel.text = @"筛选";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    //设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"Back" highImage:@"Back"];
    self.navigationItem.leftBarButtonItem = leftItem;
    

    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemClick)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    self.scrollview.delegate = self;
    self.scrollview.backgroundColor = [UIColor whiteColor];
    self.scrollview.contentSize = CGSizeMake(0, screenH);
    
    //行业
    [self addIndustryButton];
    
    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}

-(void)leftBarButtonItemClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}



-(void)rightBarButtonItemClick{
    //industry=根据行业&money=-根据金额，格式（0-5000000 亿以上：1000000000-亿）&type=根据类型（个人，企业，政府）&address= 根据地区（格式：省-市-区 如果没有就：省-无-无）
    NSMutableDictionary *dicInfo = [NSMutableDictionary dictionary];
    [dicInfo setObject:@"1" forKey:@"page"];
    if (strHY.length != 0) {//根据行业
        [dicInfo setObject:strHY forKey:@"industry"];
    }
    if (strPrice.length != 0) {//根据金额
        for (int i = 0; i<self.priceArr.count; i++) {
            if ([strPrice isEqualToString:self.priceArr[i]]) {
                //@"0-500万",@"500万-3000万",@"3000万-1亿",@"1亿以上"
                switch (i) {
                        
                    case 0:
                    {
                        [dicInfo setObject:@"" forKey:@"money"];
                    }
                        break;
                    case 1:
                    {
                        [dicInfo setObject:@"0-5000000" forKey:@"money"];
                    }
                        break;
                    case 2:
                    {
                        [dicInfo setObject:@"500000-1000000" forKey:@"money"];
                    }
                        break;
                    case 3:
                    {
                        [dicInfo setObject:@"1000000-5000000" forKey:@"money"];
                    }
                        break;
                    case 4:
                    {
                        [dicInfo setObject:@"5000000-10000000" forKey:@"money"];
                    }
                        break;
                    case 5:
                    {
                        [dicInfo setObject:@"10000000-50000000" forKey:@"money"];
                    }
                        break;
                    case 6:
                    {
                        [dicInfo setObject:@"50000000-100000000" forKey:@"money"];
                    }
                        break;
                    case 7:
                    {
                        [dicInfo setObject:@"100000000-1000000000" forKey:@"money"];
                    }
                        break;
                    case 8:
                    {
                        [dicInfo setObject:@"1000000000－亿" forKey:@"money"];
                    }
                        break;
                    
                }
            }
        }
        
    }
    
   
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"StartShaiXuanSearchPrice" object:nil userInfo:dicInfo];
    [self.navigationController popViewControllerAnimated:YES];
}
    


-(void)addIndustryButton{
    NSMutableArray *industryArr = [NSMutableArray array];
    NSMutableArray *priceArr = [NSMutableArray array];
    
    
    
    UILabel *price = [[UILabel alloc]initWithFrame:CGRectMake(10, 18, 60, 30)];
    price.text = @"金额：";
    price.textColor = [UIColor blackColor];
    price.font = [UIFont systemFontOfSize:16];
    [self.scrollview addSubview:price];
    
    for (int i = 0; i < self.priceArr.count; i++) {
        
        UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(i%3*102+12, i/3*40+price.y+price.height+8, 92, 30)];
        button.tag = i;
        [button setTitle:self.priceArr[i] forState:UIControlStateNormal];
        
        [button setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:13.0f];
        if (i ==4||i == 5) {
            button.titleLabel.font =[UIFont systemFontOfSize:11.f];
        }
        [button addTarget:self action:@selector(btnClickJinE:) forControlEvents:UIControlEventTouchUpInside];
        button.layer.masksToBounds = YES;
        button.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
        button.layer.borderWidth = 1.0f;
        button.layer.cornerRadius = 5.0f;
        [priceArr addObject:button];
        
        [self.scrollview addSubview:button];
    }
    
    UIButton *hangYebtn = priceArr.lastObject;
    
    UILabel *industry = [[UILabel alloc]initWithFrame:CGRectMake(10, hangYebtn.height + hangYebtn.y + 20, 100, 30)];
    industry.text = @"资金性质：";
    industry.textColor = [UIColor blackColor];
    industry.font = [UIFont systemFontOfSize:16];
    [self.scrollview addSubview:industry];
    
    for (int i = 0 ; i < self.industryArr.count; i++) {
        
        //创建按钮
        UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(i%3*102+12, i/3*40+industry.y +industry.height + 8, 92, 30)];
        button.tag = i;
        [button setTitle:self.industryArr[i] forState:UIControlStateNormal];
        //        button.frame = (CGRect){58+85*(i%3),18+33*(i/3),75,25};
        [button setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:13.0f];
        
        [button addTarget:self action:@selector(btnClickHangYe:) forControlEvents:UIControlEventTouchUpInside];
        button.layer.masksToBounds = YES;
        button.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
        button.layer.borderWidth = 1.0f;
        button.layer.cornerRadius = 5.0f;
        [industryArr addObject:button];
        
        [self.scrollview addSubview:button];
    }
    
}

-(void)btnClickHangYe:(UIButton *)btn{
    
    if (btn.selected) {
        btn.selected = NO;
        [btn setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
        btn.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
    }else {
        //选中
        btn.selected = YES;
        strHY = self.industryArr[btn.tag];
        [btn setTitleColor:[UIColor colorWithRed:255/255.0f green:107/255.0f blue:80/255.0f alpha:1.0f] forState:UIControlStateNormal];
        btn.layer.borderColor = [UIColor colorWithRed:255/255.0f green:107/255.0f blue:80/255.0f alpha:1.0f].CGColor;
        if (btnHY != nil) {
            btnHY.selected = NO;
            [btnHY setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
            btnHY.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
        }
        
        btnHY = btn;
    }
}

-(void)btnClickJinE:(UIButton *)btn{
    if (btn.selected) {
        btn.selected = NO;
        [btn setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
        btn.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
    }else {
        //选中
        btn.selected = YES;
        strPrice = self.priceArr[btn.tag];
        [btn setTitleColor:[UIColor colorWithRed:255/255.0f green:107/255.0f blue:80/255.0f alpha:1.0f] forState:UIControlStateNormal];
        btn.layer.borderColor = [UIColor colorWithRed:255/255.0f green:107/255.0f blue:80/255.0f alpha:1.0f].CGColor;
        if (btnPrice != nil) {
            btnPrice.selected = NO;
            [btnPrice setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
            btnPrice.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
        }
        
        btnPrice = btn;
    }
}

//-(void)btnClickAddress:(UIButton *)btn{
//    
//    if (btn.selected) {
//        btn.selected = NO;
//        [btn setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
//        btn.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
//    }else {
//        //选中
//        btn.selected = YES;
//        strAddress = self.addressArr[btn.tag];
//        [btn setTitleColor:[UIColor colorWithRed:255/255.0f green:107/255.0f blue:80/255.0f alpha:1.0f] forState:UIControlStateNormal];
//        btn.layer.borderColor = [UIColor colorWithRed:255/255.0f green:107/255.0f blue:80/255.0f alpha:1.0f].CGColor;
//        if (btnAddress != nil) {
//            btnAddress.selected = NO;
//            [btnAddress setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
//            btnAddress.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
//        }
//        
//        btnAddress = btn;
//    }
//    
//}
@end
